$(function () {
  // mui(".mui-numbox").numbox().setOption('step',1);
  var id = getSearch().id;
  console.log(id);
  $.ajax({
    url: '/product/queryProductDetail',
    type: 'get',
    data: {
      id: id
    },
    success: function (info) {
      console.log(info);
      $(".mui-scroll").html(template("tmp", info));
      // 轮播
      var gallery = mui('.mui-slider');
      gallery.slider({
        interval: 2000//自动轮播周期，若为0则不自动播放，默认为0；
      });
      // 尺码可选
      $(".mui-scroll").on("click", "span.size", function () {
        $(this).addClass("active").siblings().removeClass("active");
      })
      // 数量初始化
      mui(".mui-numbox").numbox().setOption('step', 1);


    }
  })

  $(".add_cart").on("click", function () {
    var size = $(".pro_size span.active").text();
    var num = $(".mui-numbox-input").val();


    if (!size) {
      mui.toast('请选择尺码');
      return;
    }
    $.ajax({
      url: '/cart/addCart',
      type: 'post',
      data: {
        productId: id,
        size: size,
        num: num
      },
      success: function (info) {
        console.log(info);
        if (info.success) {
          mui.confirm('添加成功', '温馨提示', ['去购物车', '继续浏览'], function (e) {
            if (e.index === 0) {
              location.href = "cart.html";
            }
          });
        }
        if (info.error === 400) {
          mui.confirm('未登录,请先登录', '温馨提示', ['继续浏览', '去登录'], function (e) {
            if (e.index === 1) {
              location.href = "login.html?backUrl=" + location.href;
             }
           }
          )
        }
      }
    })
  })
})